#!/usr/bin/python2.7
# _*_ coding: utf-8 _*_

"""
@Author: MarkLiu
"""
import numpy as np

import AdaboostNavieBayes as boostNaiveBayes
from config import *

def getTrainAdaboostInfo():
    """
    获取训练算法阶段的DS和minErrorRate信息
    :return:
    """
    trainDS = np.loadtxt(sms_ds_file, delimiter=SPLIT_TAB)
    #trainMinErrorRate = np.loadtxt('trainMinErrorRate.txt', delimiter='\t')
    #vocabularyList = boostNaiveBayes.getVocabularyList('vocabularyList.txt')
    vocabularyList = []
    f = open(sms_vocalist_file)
    for line in f.readlines():
        for word in line.strip().split(SPLIT_TAB):
            vocabularyList.append(word)
    pWordsSpamicity = np.loadtxt(sms_spam_file, delimiter=SPLIT_TAB)
    pWordsHealthy = np.loadtxt(sms_ham_file, delimiter=SPLIT_TAB)
    pSpam = np.loadtxt(spam_file, delimiter=SPLIT_TAB)
    return vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam, 0, trainDS


def databaseTest():
    # 加载训练好的模型信息
    vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam, trainMinErrorRate, trainDS = \
        getTrainAdaboostInfo()

    # 加载测试数据
    #filename = '../emails/test/test.txt'
    #smsWords, classLables = boostNaiveBayes.loadSMSData(filename)
    from database import DB
    import time
    endtime = '2017-11-1'
    allCount = DB.get_instance().getBySql_result_unique('select COUNT(1) from chat WHERE status IN (2,3) AND utime<=%s',
                                                        endtime)
    print "需要学习的总评论数:", allCount, "条"
    pageSize = 5000
    pageCount = int(allCount / pageSize) + 1
    i = 0
    errorCount = 0
    for page in range(pageCount):
        startPo = (page * pageSize)
        list = DB.get_instance().getBySql("select chat_content,status,chat_id from chat WHERE status IN (2,3) AND utime<=%s \
                      ORDER BY chat_id asc limit %s,%s",endtime,
                                          startPo, pageSize)
        for row in list:
            import re
            p = re.compile('\s+') #去除空格和换行
            strinfo = re.compile('\?') #去除？号这类没意义的字符
            new_string = re.sub(p, '', row[0])
            new_string = re.sub(strinfo, '', new_string)
            if new_string=='':
                continue
            i = i+1
            from AdaboostNavieBayes import textParser
            words = textParser(new_string)
            testWordsMarkedArray = []
            testWordsMarkedArray = boostNaiveBayes.setOfWordsToVecTor(vocabularyList, words)  # 找出这句话的分类
            #testWordsMarkedArray = np.array(testWordsMarkedArray)
            ps, ph, smsType = boostNaiveBayes.classify(
                pWordsSpamicity, pWordsHealthy, trainDS, pSpam, testWordsMarkedArray)
            if smsType == CATEGORY_SPAM and row[1] != 3:
                errorCount += 1
                print new_string
            if smsType == CATEGORY_HAMORUNKNOW and row[1] != 2:
                errorCount += 1
                print new_string
            #DB.get_instance().getBySql("update chat WHERE auto_status=%s where chat_id=%s", smsType , row[2] )
            print "完成理第%s条数据" % i
            #time.sleep(1)
    print "完成比对：%s条数据" % allCount
    print "比对错误：%s条数据" % errorCount
    print "错误率：%s" % (errorCount / allCount * 100)

def simpleTest():
    # 加载训练好的模型信息
    vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam, trainMinErrorRate, trainDS = \
        getTrainAdaboostInfo()

    # 加载测试数据
    filename = '../emails/test/test.txt'
    smsWords, classLables = boostNaiveBayes.loadSMSData(filename)
    testWordsMarkedArray = []
    testWordsMarkedArray = boostNaiveBayes.setOfWordsToVecTor(vocabularyList, smsWords[0])
    ps, ph, smsType = boostNaiveBayes.classify(
        pWordsSpamicity, pWordsHealthy, trainDS, pSpam, testWordsMarkedArray)
    if smsType == CATEGORY_SPAM:
        print "垃圾评论"
    else:
        print "正常留言"
def check(comment):
    # 加载训练好的模型信息
    vocabularyList, pWordsSpamicity, pWordsHealthy, pSpam, trainMinErrorRate, trainDS = \
        getTrainAdaboostInfo()
    import re
    p = re.compile('\s+')  # 去除空格和换行
    strinfo = re.compile('\?')  # 去除？号这类没意义的字符
    new_string = re.sub(p, '', comment)
    new_string = re.sub(strinfo, '', new_string)
    # 加载测试数据
    from AdaboostNavieBayes import textParser
    words = textParser(new_string)
    testWordsMarkedArray = boostNaiveBayes.setOfWordsToVecTor(vocabularyList, words)
    ps, ph, smsType = boostNaiveBayes.classify(
        pWordsSpamicity, pWordsHealthy, trainDS, pSpam, testWordsMarkedArray)
    return smsType
if __name__ == '__main__':
    databaseTest()
    #simpleTest()
    #print check("asdsfrg")

